package bdz.projects.cbt.core
{
	import bdz.common.model.AppEnvironment;
	import bdz.common.utils.MathUtils;
	public class CBTProduct
	{
		private var _min:Number;
		private var _max:Number;
		private var _currentPrice:Number;
		private var _canbecheap:Boolean;
		private var _canbeexpensive:Boolean;
		private var _pname:String;
		
		private var _randomize:Boolean = true;
		private var _index:Number;
		private var _currentlyCheap:Boolean = false;
		private var _currentlyExpensive:Boolean = false;
		private var _lastprice:Number;
		public function CBTProduct(xm:XML){
		
			_min = Number(xm.@min);
			_max = Number(xm.@max);
			_pname = xm.@name;
			_canbecheap = xm.@specially_cheap == "1";
			_canbeexpensive = xm.@specially_expensive == "1";
			_index = Number(xm.@index);
		}
		private function randomizePrice():void{
			
			if(_randomize){
				
				var amount:Number = _max - _min;
				var toadd:Number = Math.floor(Math.random()*amount);
				_currentlyExpensive = _currentlyCheap = false;
				/*
				if(_lastprice){
					var domin:Boolean = Boolean(Number(Math.floor(Math.random()*2)));
					var _lamount:Number;
					if(domin){
						_lamount = _lastprice-_min;
						
					}else{
						_lamount = _max-_lastprice;
					}
					_currentPrice = _lastprice+(Math.floor(Math.random()*_lamount));
					if(_currentPrice > _max || _currentPrice < _min){
						_currentPrice = _min+toadd;
					}
					
				}else{*/
					_currentPrice = _min+toadd;
				//}
				if(!_currentlyCheap && !_currentlyExpensive){
					var chance:Boolean = MathUtils.randomPercentChance(AppEnvironment.getEnv().getSetting("cheapExpensiveChance")*100);
					if(chance){
						if(_canbecheap){
							_currentlyCheap = chance;
							_currentPrice= _min-(Math.floor(Math.random()*(_min/2)));
						}
						if(_canbeexpensive){
							_currentlyExpensive = chance;
							_currentPrice= _max+(Math.floor(Math.random()*(_max/2)));
							
						}
					}
				}else{
					_currentlyCheap = _currentlyExpensive  = false;
				}
				_lastprice = _currentPrice;
			}
			
		}
		
		public function toXML():XML{
			var xm:XML = new XML("<product/>");
			xm.@index = _index.toString();
			xm.@name = _pname;
			xm.@min = _min.toString();
			xm.@max = _max.toString();
			xm.@specially_cheap = _canbecheap ? "1" : "0";
			xm.@specially_expensive = _canbeexpensive ? "1" : "0";
			return xm;
		}
		public function get index():Number{
			return _index;
		}
		public function get currentPrice():Number{
			randomizePrice();
			
			return _currentPrice;
		}
		public function set currentPrice(n:Number):void{
			_currentPrice = n;
		}
		public function get canBeSpeciallyCheap():Boolean{
			return _canbecheap;
		}
		public function get canBeSpeciallyExpensive():Boolean{
			return _canbeexpensive;
		}
		public function get name():String{
			return _pname;
		}
		public function get standardPrice():Number{
			return _currentPrice;
		}
		public function set randomize(b:Boolean):void{
			_randomize = b;
		}
		public function get randomize():Boolean{
			return _randomize;
		}
		public function get currentlyCheap():Boolean{
			return _currentlyCheap;
		}
		public function get currentlyExpensive():Boolean{
			return _currentlyExpensive;
		}
		

	}
}